import LogFactory from "../log/LogFactory.js";
import RequestHandler from "../util/RequestHadler.js";
import { IDENTITY_MAP as map} from "./constant.js";
const log = LogFactory.getLogger("LastReport");
/**
 * 最后一次成绩单
 */
export default class LastReport{
    reportlandlord = null;
    reportWinner1 = null;
    reportWinner2 = null;
    changScore = null;
    maScore = null;
    junScore = null;
    reportRound = null;
    reportConsumeTime = null; // 上一局游戏消耗时间
    reportSettlementDate = null; // 结算时间
    /**
     * 渲染需要的数据,计分数据
     */
    data = null;
    winnerArray = [];
    lastDate = null;
    async init(){
        // log.debug("初始化最后一次成绩单处理类");
        this.reportlandlord = $("#reportlandlord");
        this.reportWinner1 = $("#reportWinner1");
        this.reportWinner2 = $("#reportWinner2");
        this.changScore = $("#reportChangScore");
        this.maScore = $("#reportMaScore");
        this.junScore = $("#reportJunScore");
        this.reportRound = $("#reportRound");
        this.reportConsumeTime = $("#reportConsumeTime")
        this.reportSettlementDate = $("#reportSettlementDate");
        // await this.renderData();
    }

    /**
     * 渲染数据到页面
     * @param {*} data 最后一局游戏数据
     */
    renderData(data){
        // log.debug("渲染数据到页面: " ,data)
        this.data = data;
        if(!data){
            return;
        }
        this.parseLandlord();
        this.parseScore();
        this.parseTimeConsuming();
    }
    /**
     * 解析游戏耗时时间
     */
    parseTimeConsuming(){
        // this.lastDate = this.data.createTime;
        // log.debug("解析游戏耗时", this.lastDate);
        if(this.lastDate === null){
            // log.debug("可能是第一次,没有上一次数据");
            this.lastDate = new Date(this.data.createTime);
            return;
        }
        let newDate = new Date(this.data.createTime);
        let timeConsuming = newDate.getTime() - this.lastDate.getTime();
        let minutes = Math.floor(timeConsuming / 60000);
        let seconds = Math.floor((timeConsuming % 60000) / 1000);
        let msg = minutes + " 分钟 " + seconds + " 秒";
        this.reportConsumeTime.text(msg);
        this.lastDate = newDate;
    }

    /**
     * 解析渲染地主归属
     */
    parseLandlord(){
        this.reportRound.text(this.data.id);
        this.reportlandlord.text(map.get(this.data.landlord));
    }

    /**
     * 解析设置成绩单
     */
     parseScore(){
        this.clearAllPageData();
        let ma = this.data.ma / 100;
        let jun = this.data.jun / 100;
        let chang = this.data.chang / 100;

        if(ma > 0){
            this.winnerArray.push("MA"); // 确定胜利者
            this.maScore.css("color", "#18810d").text("+" + ma);
        }else{
            this.maScore.css("color", "#f40").text(ma);
        }

        if(jun > 0){
            this.winnerArray.push("JUN");
            this.junScore.css("color", "#18810d").text("+" + jun);
        }else{
            this.junScore.css("color", "#f40").text(jun);
        }

        if(chang > 0){
            this.winnerArray.push("CHANG");
            this.changScore.css("color", "#18810d").text("+" + chang);
        }else{
            this.changScore.css("color", "#f40").text(chang);
        }

        this.reportWinner1.text(map.get(this.winnerArray[0]));
        if(this.winnerArray.length === 2){
            this.reportWinner2.text(map.get(this.winnerArray[1]));
        }
        
        this.reportSettlementDate.text(this.data.createTime);
    }

    /**
     * 清空全部页面数据,包括内容数据 
     */
    clearAllPageData(){
        this.winnerArray = [];
    }
}